package com.example.shoppingmallclient.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.ServerIp;
import com.example.shoppingmallclient.adapter.OrderItemListAdapter;
import com.example.shoppingmallclient.vo.OrderItemVo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 袁梦达 2019012364
 */
public class ShowOrderItem extends AppCompatActivity {
    private List<OrderItemVo> list;
    private ListView listView;
    private TextView tvSumPrice;
    private Integer orderId;
    private double sumPrice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_product_list);

        Intent intent = getIntent();
        orderId = intent.getIntExtra("orderId", 1);

        list = new ArrayList<>();
        listView = findViewById(R.id.lv);
        tvSumPrice = findViewById(R.id.tv_sum_price);
        new getOrderItemList().execute();
    }

    private class getOrderItemList extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] objects) {
            try {
                String serverUrl = "http://" + ServerIp.ServerIp + ":8080/OrderDetailShowServlet";
                URL url = new URL(serverUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");

                OutputStream os = connection.getOutputStream();
                os.write(orderId.toString().getBytes());
                os.flush(); //刷新缓存

                InputStream is = connection.getInputStream();
                StringBuffer stringBuffer = new StringBuffer();
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes)) != -1){
                    stringBuffer.append(new String(bytes, 0, len));
                }

                String substring = null;
                if(stringBuffer.toString().length() > 1){
                    substring = stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
                }
                String[] strings = substring.split("#");

                for (String string : strings) {
                    System.out.println(string);
                    String[] split = string.split("@");
                    OrderItemVo orderItemVo = new OrderItemVo();
                    orderItemVo.setProductId(Integer.valueOf(split[0]));
                    String image = split[1];
                    URL imgUrl = new URL(image);
                    InputStream inputStream = imgUrl.openStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    orderItemVo.setImage(bitmap);
                    orderItemVo.setBaseInformation(split[2]);
                    orderItemVo.setPrice(Double.parseDouble((split[3])));
                    orderItemVo.setQuantity(Integer.valueOf(split[4]));
                    orderItemVo.setSumPrice(Double.parseDouble(split[5]));
                    sumPrice += Double.parseDouble(split[5]);
                    list.add(orderItemVo);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            OrderItemListAdapter orderItemListAdapter = new OrderItemListAdapter(list, R.layout.order_product_item, ShowOrderItem.this);
            listView.setAdapter(orderItemListAdapter);
            tvSumPrice.setText("订单总价：￥" + sumPrice);
        }
    }
}
